package com.zz.hhbl.config

import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.zz.framework.core.ext.fromJson
import com.zz.framework.core.ext.toJson
import com.zz.framework.core.utils.AppGlobals
import com.zz.framework.core.utils.DataStoreUtils
import com.zz.framework.core.utils.XPreference
import com.zz.framework.core.utils.language.LanguageManager
import com.zz.hhbl.entity.ChatTokenConfigEntity
import com.zz.hhbl.entity.DigitalHumanEntity
import com.zz.hhbl.entity.FenceEntity
import com.zz.hhbl.entity.LanguageEntity
import com.zz.hhbl.entity.SpotInfoEntity
import com.zz.hhbl.entity.TourGuideCommentaryEntity

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/17 20:57.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
object GlobalCache {
    private const val SP_TOKEN = "SHARED_PREFERENCES_TOKEN"
    private const val SHARED_PREFERENCES_AI_CONFIG = "SHARED_PREFERENCES_AI_CONFIG"
    private const val SHARED_PREFERENCES_FIRST_LAUNCH = "SHARED_PREFERENCES_FIRST_LAUNCH_V1"
    private const val SHARED_PREFERENCES_LANGUAGE_LIST = "SHARED_PREFERENCES_LANGUAGE_LIST"
    private const val SHARED_PREFERENCES_APP_VERSION = "SHARED_PREFERENCES_APP_VERSION"
    private const val SHARED_PREFERENCES_SPOT_VERSION = "SHARED_PREFERENCES_SPOT_VERSION"
    private const val SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY_VERSION = "SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY_VERSION"
    private const val SHARED_PREFERENCES_DIGITAL_HUMAN_VERSION = "SHARED_PREFERENCES_DIGITAL_HUMAN_VERSION"
    private const val SHARED_PREFERENCES_ELECTRONIC_FENCE_VERSION = "SP_ELECTRONIC_FENCE_VERSION"
    private const val SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY = "SP_TOUR_GUIDE_COMMENTARY_"
    private const val SHARED_PREFERENCES_DIGITAL_HUMAN = "SP_DIGITAL_HUMAN_"
    private const val SHARED_PREFERENCES_SPOT_INFO = "SHARED_PREFERENCES_SPOT_INFO_"
    private const val SHARED_PREFERENCES_FENCE = "SHARED_PREFERENCES_FENCE_"
    private const val SHARED_PREFERENCES_LATITUDE = "SHARED_PREFERENCES_LATITUDE"
    private const val SHARED_PREFERENCES_LONGITUDE = "SHARED_PREFERENCES_LONGITUDE"
    private const val SHARED_PREFERENCES_VOICE_TONE = "SHARED_PREFERENCES_VOICE_TONE"



    var aiVoiceTone by XPreference(SHARED_PREFERENCES_VOICE_TONE, "zh_female_qingxinnvsheng_mars_bigtts") // AI音色

    fun isFirstLaunch() = DataStoreUtils.getSyncData(SHARED_PREFERENCES_FIRST_LAUNCH, true)
    fun setIsFirstLaunch(boolean: Boolean) {
        DataStoreUtils.putSyncData(SHARED_PREFERENCES_FIRST_LAUNCH, boolean)
    }
    fun getLatitude() = DataStoreUtils.getSyncData(SHARED_PREFERENCES_LATITUDE, 0.0)
    fun setLatitude(latitude: Double) {
        DataStoreUtils.putSyncData(SHARED_PREFERENCES_LATITUDE, latitude)
    }

    fun getLongitude() = DataStoreUtils.getSyncData(SHARED_PREFERENCES_LONGITUDE, 0.0)
    fun setLongitude(longitude: Double) {
        DataStoreUtils.putSyncData(SHARED_PREFERENCES_LONGITUDE, longitude)
    }

    fun getAiConfig(language: String = LanguageManager.currentLangCode) = DataStoreUtils.getSyncData(SHARED_PREFERENCES_AI_CONFIG + language, "{}").fromJson<ChatTokenConfigEntity>()
    fun setAiConfig(entity: ChatTokenConfigEntity?, language: String = LanguageManager.currentLangCode) {
        if (entity != null) {
            DataStoreUtils.putSyncData(SHARED_PREFERENCES_AI_CONFIG + language, entity?.toJson().orEmpty())
        }
    }

    fun getFence(language: String = LanguageManager.currentLangCode): List<FenceEntity> {
        val list = DataStoreUtils.getSyncData(SHARED_PREFERENCES_FENCE + language, "[]").fromJson<List<FenceEntity>>()
        return list
    }

    fun setFence(list: List<FenceEntity>?, language: String = LanguageManager.currentLangCode) {
        DataStoreUtils.putSyncData(SHARED_PREFERENCES_FENCE + language, list?.toJson().orEmpty())
    }


    fun getSpotInfo(language: String = LanguageManager.currentLangCode) = DataStoreUtils.getSyncData(SHARED_PREFERENCES_SPOT_INFO + language, "{}").fromJson<SpotInfoEntity>()
    fun setSpotInfo(entity: SpotInfoEntity?, language: String = LanguageManager.currentLangCode) {
        DataStoreUtils.putSyncData(SHARED_PREFERENCES_SPOT_INFO + language, entity?.toJson().orEmpty())
    }

    fun getDigitalHuman(language: String = LanguageManager.currentLangCode) = DataStoreUtils.getSyncData(SHARED_PREFERENCES_DIGITAL_HUMAN + language, "[]").fromJson<List<DigitalHumanEntity>>()
    fun setDigitalHuman(list: List<DigitalHumanEntity>?, language: String = LanguageManager.currentLangCode) {
        if (list.isNullOrEmpty().not()) {
            DataStoreUtils.putSyncData(SHARED_PREFERENCES_DIGITAL_HUMAN + language, list?.toJson().orEmpty())
        }
    }

    fun getTourGuideCommentary(language: String = LanguageManager.currentLangCode): List<TourGuideCommentaryEntity> {
        val list = DataStoreUtils.getSyncData(SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY + language, "[]").fromJson<List<TourGuideCommentaryEntity>>()
        return list
    }

    fun setTourGuideCommentary(entity: List<TourGuideCommentaryEntity>?, language: String = LanguageManager.currentLangCode) {
        if (entity.isNullOrEmpty().not()) {
            DataStoreUtils.putSyncData(SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY + language, entity?.toJson().orEmpty())
        }
    }


    var spLanguageList: String by XPreference(SHARED_PREFERENCES_LANGUAGE_LIST, "[]")
    fun getLanguageList() = spLanguageList.fromJson<List<LanguageEntity>>()
    fun setLanguageList(entity: List<LanguageEntity>?) {
        spLanguageList = entity?.toJson().orEmpty()
    }


    private var spAppVersion: String by XPreference(SHARED_PREFERENCES_APP_VERSION, "")
    private var spSpotVersion: String by XPreference(SHARED_PREFERENCES_SPOT_VERSION, "")
    private var spTourGuideCommentaryVersion: String by XPreference(SHARED_PREFERENCES_DIGITAL_HUMAN_VERSION, "")
    private var spDigitalHumanVersion: String by XPreference(SHARED_PREFERENCES_TOUR_GUIDE_COMMENTARY_VERSION, "")
    private var spElectronicFenceVersion: String by XPreference(SHARED_PREFERENCES_ELECTRONIC_FENCE_VERSION, "")

    fun getAppVersion() = spAppVersion
    fun setAppVersion(version: String) {
        spAppVersion = version
    }

    fun getSpotVersion() = spSpotVersion
    fun setSpotVersion(version: String) {
        spSpotVersion = version
    }

    fun getTourGuideCommentaryVersion() = spTourGuideCommentaryVersion
    fun setTourGuideCommentaryVersion(version: String) {
        spTourGuideCommentaryVersion = version
    }

    fun getDigitalHumanVersion() = spDigitalHumanVersion
    fun setDigitalHumanVersion(version: String) {
        spDigitalHumanVersion = version
    }

    fun getElectronicFenceVersion() = spElectronicFenceVersion
    fun setElectronicFenceVersion(version: String) {
        spElectronicFenceVersion = version
    }

    private var spToken: String by XPreference(SP_TOKEN, "")
    fun getToken(): String {
        return spToken
    }

    fun setToken(token: String? = "") {
        spToken = token.orEmpty()
    }

    fun precacheImage(url: String?) {
        Glide.with(AppGlobals.getApplication())
            .applyDefaultRequestOptions(RequestOptions().override(Target.SIZE_ORIGINAL))
            .load(url)
            .preload()
    }
}